package com.isoft.demo01.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.demo01.dao.impl.GameManageRepository;
import com.isoft.demo01.dao.impl.GameRepository;
import com.isoft.demo01.entity.Game;
import com.isoft.demo01.service.GameManageService;

import com.isoft.demo01.util.FileUtil;
import com.isoft.demo01.util.IdUtil;
import com.isoft.demo01.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Arrays;
import java.util.List;

@Service("gameServiceSys")
public class GameManageServiceImpl implements GameManageService {
    @Autowired
    GameManageRepository gameManageRepository;

    @Override
    public boolean add(Game game) {
        if (null==game){
            return false;
        }
        if (StringUtil.isEmpty(game.getGamename())){
            return false;
        }
        return gameManageRepository.insert(game)>0;
    }

    @Override
    public boolean delById(Integer gameid, String gamePhotoPath, HttpServletRequest request) {
        if(!IdUtil.idcheck(gameid)){
            return false;
        }
        Game g=gameManageRepository.getPhotoById(gameid);
        boolean r=gameManageRepository.deleteById(gameid)>0;
        if (r){
            delFiles(g,gamePhotoPath,request);

        }
        return r;
    }

    @Override
    public boolean delByIds(Integer[] gameids, String gamePhotoPath, HttpServletRequest request) {
        if (null==gameids||gameids.length ==0){
            return false;
        }
        List<Game>gameList=gameManageRepository.getPhotoByIds(Arrays.asList(gameids));
        boolean r=gameManageRepository.deleteByIds(Arrays.asList(gameids))>0;
        for (Game g:gameList){
            delFiles(g,gamePhotoPath,request);
        }
        return r;
    }

    @Override
    public boolean update(Game game) {
        if (null == game) {
            return false;
        }
        if (!IdUtil.idcheck(game.getGameid())) {
            return false;
        }
        Game g=new Game();
        g.setGameid(game.getGameid());
        g.setGamename(game.getGamename());
        g.setTypeid(game.getTypeid());
        g.setPrice(game.getPrice());
        g.setPhoto(game.getPhoto());
        g.setIntroduce(game.getIntroduce());
        return gameManageRepository.updateById(g)>0;
    }

    @Override
    public Page<Game> getMore(Integer current, Integer size, String gamename, String introduce, Integer typeid) {
        if(null == current || current < 1) {
            current = 1 ;
        }
        if(null == size || size < 1) {
            size = 10 ;
        }
        if (StringUtil.isEmpty(gamename)){
            gamename=null;
        }
        if (StringUtil.isEmpty(introduce)){
            introduce=null;
        }
        if (null==typeid||typeid<1){
            typeid=null;
        }
        Page<Game> page =gameManageRepository.page(current,size,gamename,typeid,introduce);
        return page;
    }

    @Override
    public Game getById(Integer gameid) {
        if (!IdUtil.idcheck(gameid)){
            return null;
        }
        return gameManageRepository.getDetailById(gameid);
    }

    public void delFiles(Game g , String gamePhotoPath , HttpServletRequest request) {
        if (null!=g&&g.getPhoto()!=null&&g.getPhoto().trim().length()>0){
            String photo=g.getPhoto().trim();
            String[]img=photo.split(",");
            for (String imgFile:img){
                System.out.println(FileUtil.getAbsPath(gamePhotoPath,request));
                File f=new File(FileUtil.getAbsPath(gamePhotoPath,request),imgFile.trim());
                if (f.exists()){
                    System.out.println(f.delete());
                }
            }
        }

    }

}
